<?php
if( !defined('IN') )
	define('IN', TRUE);

	require_once 'common.php';
    if( !empty($_GET['id']) AND is_numeric($_GET['id']) ) {
        $tabdata = $enchersObj->getEnchereById($_GET['id']);
        
        foreach ($tabdata as $data) {
           $data['pc']=$data['limite']==0?'b':'';
		   if( !isset($data['ae']) || $data['ae'] == 0 ) $data['ae'] = 'b';
				$data['AE'] = 'AE';
                
		   $data['enchere_g'] = (float)$data['enchere']>(float)( $data['prix']*$data['limite']/100)?' enchere_g':'';
		   $data['enchere'] = str_replace('.', ',', $data['enchere']);
           
           list($datee, $heuree) = explode(' ', $data['dateFin']);
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0]));
           
		   $time_en = (intval( $data['begin'])==0?$data['duree']:$datef - time());
		   $data['timeEn'] = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_; 
		   $val = $data['retime'];
		   $data['retime'] =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
		   $data['pseudo'] = (($data['pseudo']!=null)?$data['pseudo']:'Aucun');
		   if(intval($data['begin'])==0){
			   $enchersObj->updateBeginEnchere(array( 'id'=>$data['id'],'dateFin'=>date('Y-m-d H:i:s',time()+intval($data['duree']))));
		   }
           $data['type'] = ($data['termine']==1 || $time_en<0?'termine':($time_en==$data['duree']?'avenir':'encours'));
?>
<div id="<?php echo $data['type'] ?>-prod">
    <div class="icons">
        <div class="iconu">
            <span class="rt"><?php  echo $data['retime']; ?></span>
        </div>
        <div class="iconu">
            <span class="pc<?php  echo $data['pc']; ?>"><?php  echo $data['limite']; ?>%</span>
        </div>
        <div class="iconu">
            <span class="ae<?php  echo $data['ae']; ?>"><?php  echo $data['AE']; ?></span>
        </div>
    </div>
    <div class="titre"><?php  echo $data['nom']; ?></div>
    <div class="temps">
        <?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
            EN PAUSE
        <?php else: ?>
            <?php echo $data['timeEn'] ?>
        <?php endif; ?>
    </div>
    <div class="enchere<?php echo $data['enchere_g']; ?>"><?php  $valEnchere = $data['enchere']; echo $data['enchere']; ?> <?php echo $site->getDevise(); ?></div>
    <div class="encherir">
        <?php if($data['timeEn'] == _TERMINE_): ?>
            <img src="<?php  echo _THEME_IMG_DIR_ ?>big-terminee.jpg" alt="Indisponible" />
        <?php else: ?>
            <?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
                <img src="<?php  echo _THEME_IMG_DIR_ ?>big-avenir.jpg" alt="Indisponible" />
            <?php elseif($user->isAuthenticated() AND $data['meneur'] == $user->getId()): ?>
                <img src="<?php  echo _THEME_IMG_DIR_ ?>bt-meneur.jpg" alt="Meneur" />
            <?php else: ?>
                <a class="encherir-elt" href="#" id="encherir-<?php  echo $data['id'].'-'.($user->isAuthenticated()?intval($user->getId()):0); ?>"><img src="<?php  echo _THEME_IMG_DIR_ ?>big-encherir.jpg" alt="Ench&eacute;rir" /></a>
            <?php endif; ?>
        <?php endif; ?>
    </div>
    <div class="infos">
        <?php if( $data['type'] == 'termine' ): ?>
            <span class="meneur"><?php echo _GAGNANT_ ?></span> <span class="pseudo"><?php  echo $data['pseudo']; ?></span>
        <?php elseif($data['type'] == 'avenir'): ?>
            <div class="prix"><?php echo _PRIX_MAGASIN_ ?><?php  echo $data['prix']; ?> <?php echo $site->getDevise(); ?></div>
        <?php else: ?>
            <div class="prix"><?php echo _PRIX_MAGASIN_ ?><?php  echo $data['prix']; ?> <?php echo $site->getDevise(); ?></div>
            <span class="meneur"><?php echo _MENEUR_ ?></span> <span class="pseudo"><?php  echo $data['pseudo']; ?></span>
        <?php endif; ?>
    </div>
</div>
<div class="clear"></div>
<?php
        }
    }
    
    
?>
<script type="text/javascript">
// <![CDATA[
( function($) { 
    $(document).ready(function(){
        $('.encherir-elt').unbind('load').click(function(){
            var idelt = this.id;
            idelt = idelt.split('-');
            iduser =idelt.pop();
            nbre_credits = $('#nber_cdt').html();
            nbre_credits = Number(nbre_credits);
            if(iduser==0 || nbre_credits==0){
                alert("<?php echo _INFOS_ENCHERIR_ ?>");
                return false;
            }else{
                idpro =idelt.pop();
                $.ajax({
                    type: "POST", 
                    url: "traitemntajax.html",
                    data: "iduser="+iduser+"&idprod="+idpro+"&cred="+nbre_credits,
                    cache: false,
                    success:  function(response){
                        $('#nber_cdt').html(nbre_credits-1);
                    },

                    onFailure: function(xhr){
                        alert("Erreur lors du placement de l'ench&egrave;re");
                    },
                })
            }
            return false;
        });
    });
} ) ( jQuery )
//]]>
</script>